package com.atguigu.guliguli.product.controller;

import com.atguigu.guliguli.common.utils.R;
import com.atguigu.guliguli.product.entity.BrandEntity;
import com.atguigu.guliguli.product.entity.CategoryBrandRelationEntity;
import com.atguigu.guliguli.product.service.CategoryBrandRelationService;
import com.atguigu.guliguli.product.vo.BrandVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: 叶子
 * @date: 2021/11/27 16:01
 **/
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

    /**
     * 获取当前品牌关联的所有分类列表
     */
    @GetMapping("/catalog/list")
    public R catalogList(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> data =
                categoryBrandRelationService.list(new QueryWrapper<CategoryBrandRelationEntity>()
                        .eq("brand_id", brandId));
        return R.ok().put("data", data);
    }
    /**
     * /product/categorybrandrelation/brands/list
     * 1.Controller: 处理请求，接收和校验数据
     * 2.Service接收Controller传来的数据，进行业务处理
     * 3.Controller接收Service处理完的数据，封装页面指定的vo
     */
    @GetMapping("/brands/list")
    public R relationBrandsList (@RequestParam(value = "catId",required = true) Long catId){
        List<BrandEntity> vos = categoryBrandRelationService.getBrandsByCatId(catId);
        List<BrandVo> collect = vos .stream().map(item ->{
            BrandVo brandVo = new BrandVo();
            brandVo.setBranId(item.getBrandId());
            brandVo.setBrandName(item.getName());
            return brandVo;
        }).collect(Collectors.toList());
        return R.ok().put("data",collect);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }
}